import mysql.connector
from mysql.connector import errorcode
from src.log import logger
import time
import pandas as pd

pd.set_option('display.expand_frame_repr', False)
pd.set_option('display.max_rows', None)


class Tools:
    interviewers_list = []
    start_time_list = []
    test_total_number_list = []
    right_answer_number_list = []
    during_time_list = []
    right_rate_list = []
    relative_right_rate_list = []

    def clear(self):
        self.interviewers_list = []
        self.start_time_list = []
        self.test_total_number_list = []
        self.right_answer_number_list = []
        self.during_time_list = []
        self.right_rate_list = []
        self.relative_right_rate_list = []

    def __init__(self):
        self.config = {
            # 'user': 'iroan',
            'user': 'root',
            'password': 'dpl_root',
            # 'password': 'iroanMYS_47',
            'host': '127.0.0.1',
            'database': 'label1',
            'use_pure': False  # 不是有纯Python实现（使用C扩展）
        }
        try:
            self.cnx = mysql.connector.connect(**self.config)
            logger.debug('already connect to MySql')

        except mysql.connector.Error as err:
            if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
                logger.error('mysql数据库的用户名或者密码错误！')
            elif err.errno == errorcode.ER_BAD_DB_ERROR:
                logger.error('连接的数据库不存在！')
            else:
                logger.error(err)

    def get_mysql_connection(self):
        return self.cnx

    def getTestRightAnswer(self):
        cur = self.cnx.cursor()
        cur.execute('select CaseID,Answer from right_answer;')
        data = tuple(cur)
        cur.close()
        return data

    def show(self, result):
        self.interviewers_list.append(result['interviewerID'])
        x = time.localtime(result['start_time'])
        self.start_time_list.append(time.strftime('%Y-%m-%d %H:%M:%S', x))
        self.test_total_number_list.append(result['test_total_number'])
        self.right_answer_number_list.append(result['right_answer_number'])
        self.during_time_list.append(result['during_time'])
        self.right_rate_list.append(result['right_rate'])
        self.relative_right_rate_list.append(result['relative_right_rate'])

    def beautify_show(self):
        interviewerID = pd.Series(self.interviewers_list)
        start_time = pd.Series(self.start_time_list)
        test_total_number = pd.Series(self.test_total_number_list)
        right_answer_number = pd.Series(self.right_answer_number_list)
        during_time = pd.Series(self.during_time_list)
        right_rate = pd.Series(self.right_rate_list)
        relative_right_rate = pd.Series(self.relative_right_rate_list)

        col=pd.DataFrame({'名字':interviewerID,'开始时间':start_time,'总数':test_total_number,'作对':right_answer_number,'用时':during_time,'准确率':right_rate,'相对准确率':relative_right_rate})
        temp= col.sort_values(by='开始时间', ascending=False)
        temp.index = ['']*temp.shape[0]
        return temp